/* 
vue 过滤器
*/
 
import * as math from 'mathjs';
import currencyConfig from "@/config/currency";
// import { useGlobalStore } from "@/stores/modules/global";
// console.log(currencyConfig)
// console.log(useGlobalStore)
// let global=useGlobalStore();
// console.log(global);
const fns = {
    currencyFormat: function (amount: string, options: {}) {
        console.log(amount)
        let defaultOptions = {
            currency: "",
            split: true,
            toFixed: 4
        }
        defaultOptions = { ...defaultOptions, ...options };
        if (defaultOptions.toFixed) {
            amount = fns.currencyFixed(amount, defaultOptions.toFixed)
        }
        console.log(amount)
        if (defaultOptions.split) {
            amount = fns.currencySplit(amount)
        }
        if (defaultOptions.currency) {
            amount = fns.currencySet(amount, defaultOptions.currency)
        }
        amount=fns.currencyReplace(amount);

        return amount
    },
    currencySplit(amount: string) {
        //数字三位分割方法
        let negativeNumber = '';
        let numSplit = [];
        const reg = /\B(?=(\d{3})+(?!\d))/g;
        if (!fns.currencyIsNumber(amount)) {
            console.warn("传入参数值非合法数字")
            return amount
        }
        numSplit = amount.split('.');
        console.log(numSplit)
        numSplit[0] = numSplit[0].replace(reg, ' ')
        console.log(numSplit)
        return numSplit.join('.');
    },
    currencyReplace(amount){
        let divisionIden =window.merak_language == "zh" ? "." : ","
        return amount.replace('.',divisionIden);
    },
    currencyFixed(amount: string, toFixed) {
        //金额保留小数位数，不做四舍五入  
        if (!fns.currencyIsNumber(amount)) {
            console.warn("传入参数值非合法数字")
            return amount
        }
        let amountSplit = amount.split(".");
        if (amountSplit.length > 1) {
            amountSplit[1] = amountSplit[1].substring(1, toFixed+1)
        } 
        return amountSplit.join(".")
    },
    currencySet(amount: string, currency) {
        let lowerCase = currency.toLowerCase()
        if (lowerCase == "rub") {
            return amount + currencyConfig[lowerCase];
        }
        return currencyConfig[lowerCase] + amount;

    },
    currencyIsNumber(num: string) {//合法数字验证，不接受0开头整数
        const numReg = /^-?([1-9]\d*|0)(\.\d+)?$/;
        return numReg.test(num)
    }
}
const injectFliter = (vueInstance: any) => {
    Object.keys(fns).map(key => {
        vueInstance.config.globalProperties[`$${key}`] = fns[key];

    })
}

export default injectFliter